<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\GotchaAdmin\Controllers;

use Gotcha\App\Extensions\GotchaCulturesExtension;
use Gotcha\App\Extensions\GotchaErrorsExtension;
use Gotcha\App\Extensions\GotchaLogsExtension;
use Gotcha\App\Extensions\HttpExtension;
use Gotcha\App\Framework\Controller;
use Gotcha\App\Types\String;
use GotchaDatabase\GotchaContent;
use GotchaDatabase\GotchaContentQuery;

class StaticContentController extends Controller {
    public function Index() {
        return parent::View();
    }

    public function Create() {
        return parent::View();
    }

    public function CreatePost() {
        $http = new HttpExtension();

        if(empty($_POST['StrongName']) || empty($_POST['Title']) || empty($_POST['Text']) || !isset($_POST['StrongName']) || !isset($_POST['Title']) || !isset($_POST['Text'])) {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent/Create?error=1'));
            exit;
        }

        // Add new content
        try {
            $static = new GotchaContent();
            $static->setStrongName($_POST['StrongName']);
            $static->setTitle($_POST['Title']);
            $static->setText($_POST['Text']);

            // Set date
            $date = new \DateTime();

            $static->setCreationDate($date->format('Y-m-d'));
            $static->setCreationTime($date->format('H:i:s'));

            // Hardcode user
            $static->setGotchaUsersId(1);
            $static->save();
        } catch (\Exception $e) {
            GotchaLogsExtension::createNewLogEntry(new String($e->getMessage()));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_EXECUTING_QUERY'))));
            exit;
        }

        header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent'));
        exit;
    }

    public function Edit() {
        return parent::View();
    }

    public function EditPost() {
        $http = new HttpExtension();

        // Validate input
        if(empty($_POST['Id']) || !isset($_POST['Id'])) {
            die('Error');
        }

        if(empty($_POST['StrongName']) || empty($_POST['Title']) || empty($_POST['Text']) || !isset($_POST['StrongName']) || !isset($_POST['Title']) || !isset($_POST['Text'])) {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent/Edit?id='.$_POST['Id'].'error=1'));
            exit;
        }

        // Update
        try {
            $static = GotchaContentQuery::create()
                        ->findOneById($_POST['Id'])
                            ->setStrongName($_POST['StrongName'])
                                ->setTitle($_POST['Title'])
                                    ->setText($_POST['Text'])
                                        ->save();
        } catch (\Exception $e) {
            GotchaLogsExtension::createNewLogEntry(new String($e->getMessage()));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_EXECUTING_QUERY'))));
            exit;
        }

        header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent'));
        exit;
    }

    public function Delete() {
        $http = new HttpExtension();

        // Validate input
        if(empty($_GET['id']) || !isset($_GET['id'])) {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent?error=2'));
            exit;
        }

        // Delete element
        try {
            $static = GotchaContentQuery::create()
                        ->findOneById($_GET['id'])
                            ->delete();
        } catch (\Exception $e) {
            GotchaLogsExtension::createNewLogEntry(new String($e->getMessage()));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_EXECUTING_QUERY'))));
            exit;
        }

        header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/StaticContent'));
        exit;
    }

    public function returnStaticContent() {
        $staticContent = GotchaContentQuery::create()
                            ->orderById()
                                ->find();

        // String builder
        $html = '';
        foreach($staticContent as $content) {
            $html .= '<tr>';
            $html .= sprintf('<td>%s</td><td>%s</td><td>%s</td><td>[ <a href="%s?id=%s">Edytuj</a> | <a href="%s?id=%s" class="delete">Usuń</a> ]</td>', $content->getTitle(), $content->getStrongName(), $content->getCreationDate().' '.$content->getCreationTime(), parent::UrlContent('/Admin/StaticContent/Edit'), $content->getId(), parent::UrlContent('/Admin/StaticContent/Delete'), $content->getId());
            $html .= '</tr>';
        }

        return $html;
    }

    public function returnContent($id) {
        $content = GotchaContentQuery::create()
                    ->findOneById($id);
        return $content;
    }
}